<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>商品管理</title>
    <link href="./css/main.css" rel="stylesheet"/>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="./css/element.css"/>
</head>

<body>
<div id="app">
    <el-container>
        <el-header>
            <span class="s1">黑马商城管理系统</span>
            <span class="s2">管理员 <i class="el-icon-switch-button" style="margin-right: 20px"></i></span>
        </el-header>

        <el-container>
            <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
                <el-menu>
                    <el-submenu index="1">
                        <template slot="title"><i class="el-icon-s-promotion"></i>首页</template>
                        <el-menu-item index="1-3"><i class="el-icon-position"></i>工作台</el-menu-item>
                    </el-submenu>

                    <el-submenu index="2">
                        <template slot="title"><i class="el-icon-s-goods"></i>商品管理</template>
                        <el-menu-item index="2-1"><i class="el-icon-magic-stick"></i>分类管理</el-menu-item>
                        <el-menu-item index="2-2"><i class="el-icon-goods"></i>商品管理</el-menu-item>
                        <el-menu-item index="2-3" @click="toOrder"><i class="el-icon-s-claim"></i>订单管理
                        </el-menu-item>
                    </el-submenu>

                    <el-submenu index="3">
                        <template slot="title"><i class="el-icon-s-tools"></i>权限管理</template>
                        <el-menu-item index="3-1"><i class="el-icon-user"></i>用户管理</el-menu-item>
                        <el-menu-item index="3-2"><i class="el-icon-goods"></i>角色管理</el-menu-item>
                        <el-menu-item index="3-3"><i class="el-icon-pie-chart"></i>菜单管理</el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <el-button type="primary" size="small" @click="beginAdd">新增商品</el-button>
                <br/><br/>

                <el-table :data="items" border style="width: 100%">
                    <el-table-column prop="id" label="编号" width="120" align="center"></el-table-column>

                    <el-table-column prop="image" label="图片" width="100" align="center">
                        <template slot-scope="scope">
                            <el-image style="width: 50px; height: 50px" :src="scope.row.image" fit="fill"
                                      :preview-src-list="[scope.row.image]"></el-image>
                        </template>
                    </el-table-column>

                    <el-table-column prop="name" label="名称" align="center"></el-table-column>
                    <el-table-column prop="category" label="分类" width="100" align="center"></el-table-column>
                    <el-table-column prop="brand" label="品牌" width="100" align="center"></el-table-column>
                    <el-table-column prop="price" label="价格" width="100" align="center"></el-table-column>

                    <el-table-column prop="spec" label="规格" width="150" align="center">
                        <template slot-scope="scope">
                            <div v-for="(v, k) in parseJSON(scope.row.spec)">{{k}} : {{v}}</div>
                        </template>
                    </el-table-column>

                    <el-table-column prop="status" label="状态" width="80" align="center">
                        <template slot-scope="scope"> {{scope.row.status==1?'上架':'下架'}}</template>
                    </el-table-column>

                    <el-table-column prop="isAD" label="是否广告" width="80" align="center">
                        <template slot-scope="scope"> {{scope.row.isAD==1?'是':'否'}}</template>
                    </el-table-column>

                    <el-table-column align="center" label="操作" :width="160">
                        <template slot-scope="scope">
                            <el-button type="primary" plain icon="el-icon-edit" circle size="mini"
                                       @click="handleEdit(scope.$index, scope.row)"></el-button>
                            <el-button type="danger" plain icon="el-icon-delete" circle size="mini"
                                       @click="handleDelete(scope.$index, scope.row)"></el-button>
                            <el-tooltip class="item" v-if="scope.row.status === 2" effect="dark" content="上架"
                                        placement="top-start">
                                <el-button type="success" plain icon="el-icon-upload2" circle size="mini"
                                           @click="handleUp(scope.row.id)"></el-button>
                            </el-tooltip>
                            <el-tooltip class="item" v-else effect="dark" content="下架" placement="top-start">
                                <el-button type="info" plain icon="el-icon-download" circle size="mini"
                                           @click="handleDown(scope.row.id)"></el-button>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                </el-table>

                <br/>
                <!-- 分页条 -->
                <el-pagination @current-change="query" style="margin-top: 5px" :page-size="5" layout="prev, pager, next"
                               :total="total"></el-pagination>
            </el-main>
        </el-container>
    </el-container>

    <!-- 新增、编辑对话框 -->
    <el-dialog title="商品信息" :visible.sync="formVisible" width="50%" style="padding: 0 20px">
        <el-form :model="item" :inline="true" size="small" label-position="left" :label-width="formLabelWidth">
            <el-form-item label="商品名称" style="width: 100%">
                <el-input
                        type="textarea"
                        v-model="item.name"
                        autocomplete="off"
                        placeholder="请输入内容"
                        maxlength="60"
                        :rows="3"
                        show-word-limit
                        style="width: 500px"></el-input>
            </el-form-item>
            <el-form-item label="商品分类">
                <el-input v-model="item.category" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="商品品牌">
                <el-input v-model="item.brand" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="商品价格">
                <el-input v-model="item.price" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="商品库存">
                <el-input v-model="item.stock" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="商品规格">
                <el-input v-model="item.spec" autocomplete="off" readonly></el-input>
            </el-form-item>
            <el-form-item label="是否广告">
                <el-switch v-model="item.isAD"></el-switch>
            </el-form-item>
            <el-form-item label="商品图片" style="width: 100%">
                <el-upload
                        class="avatar-uploader"
                        action="https://jsonplaceholder.typicode.com/posts/"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess">
                    <img v-if="item.image" :src="item.image" class="avatar"/>
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="formVisible = false" size="medium">取 消</el-button>
            <el-button type="primary" @click="confirmEdit" size="medium">确 定</el-button>
        </div>
    </el-dialog>
</div>

<script src="./js/vue.js"></script>
<script src="./js/axios.min.js"></script>
<!-- 引入组件库 -->
<script src="./js/element.js"></script>
<script>
    // 设置后台服务地址
    axios.defaults.baseURL = 'http://localhost:10010'
    axios.defaults.timeout = 3000

    const app = new Vue({
        el: '#app',
        data: {
            items: [],
            total: 1000,
            formVisible: false, // 是否显示表单
            formLabelWidth: '100px', // 表单label宽度
            item: {
                isAD: false,
                image: '',
            }, // 表单中的酒店数据
            isEdit: false, // 是否是更新
            lastPage: 1, // 上一次查询的页码
        },

        created() {
            this.query(1)
        },

        methods: {
            toOrder() {
                location.href = 'order.html'
            },
            beginAdd() {
                this.isEdit = false
                this.item = {
                    spec: '{"颜色": "黑色"}',
                }
                this.formVisible = true
            },
            query(page) {
                this.lastPage = page
                axios
                    .get('/item/list', {
                        params: {
                            page: page,
                            size: 5,
                        },
                    })
                    .then((resp) => {
                        this.items = resp.data.list
                        this.total = resp.data.total
                    })
                    .catch((err) => console.log(err))
            },
            handleEdit(v1, v2) {
                this.isEdit = true
                this.item = JSON.parse(JSON.stringify(v2))
                this.formVisible = true
            },

            handleDelete(v1, v2) {
                this.$confirm('此操作将永久删除商品信息, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(() => {
                        this.deleteById(v2.id)
                    })
                    .catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除',
                        })
                    })
            },

            confirmEdit() {
                if (this.isEdit) {
                    axios
                        .put('/item', this.item)
                        .then((resp) => {
                            this.$message({
                                message: '更新成功',
                                type: 'success',
                            })
                            this.formVisible = false
                            this.reload()
                        })
                        .catch((err) => {
                            this.$message({
                                message: '更新失败',
                                type: 'error',
                            })
                            console.log(err)
                        })
                } else {
                    axios
                        .post('/item', this.item)
                        .then((resp) => {
                            this.$message({
                                message: '新增成功',
                                type: 'success',
                            })
                            this.formVisible = false
                            this.reload()
                        })
                        .catch((err) => {
                            this.$message({
                                message: '新增失败',
                                type: 'error',
                            })
                            console.log(err)
                        })
                }
            },

            deleteById(id) {
                axios
                    .delete('/item/' + id)
                    .then(() => {
                        this.$message({
                            type: 'success',
                            message: '删除成功!',
                        })
                        this.reload()
                    })
                    .catch((err) => {
                        this.$message({
                            type: 'error',
                            message: '删除失败!',
                        })
                        console.log(err)
                    })
            },
            handleUp(id) {
                this.updateStatus(id, 1)
            },
            handleDown(id) {
                this.updateStatus(id, 2)
            },
            updateStatus(id, status) {
                let action = status === 1 ? '上架' : '下架'

                axios
                    .put('/item/status/' + id + '/' + status)
                    .then(() => {
                        this.$message({
                            type: 'success',
                            message: action + '成功!',
                        })
                        this.reload()
                    })
                    .catch((err) => {
                        this.$message({
                            type: 'error',
                            message: action + '失败!',
                        })
                        console.log(err)
                    })
            },

            reload() {
                this.query(this.lastPage)
            },

            parseJSON(str) {
                return JSON.parse(str)
            },
        },
    })
</script>
</body>
</html>
